package com.sl.au.controller;


import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;

import com.sl.au.entity.SinglePump;
import com.sl.au.service.SinglePumpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.sl.au.entity.Account;
import com.sl.au.entity.Device;
import com.sl.au.entity.Module;
import com.sl.au.service.AttachmentService;
import com.sl.au.service.DeviceService;

@RestController
@RequestMapping(value = "/ui/priceBook/singlePump")
public class SinglePumpController extends BaseController {

    @Autowired
    SinglePumpService singlePumpService;
    
    @Autowired
    DeviceService deviceService;

    @Autowired
    AttachmentService attachmentService;


    @RequestMapping(method = RequestMethod.GET)
    public ModelAndView index() {
        ModelAndView modelAndView = createLayoutView("priceBook/singlePump/singlePumpIndex");
        Account a = this.getMyAccount();
        String name="singlePump"; 
        Module module = this.getPageModule(name);
        String superior = a.getUser().getSaleArea().getSuperior();
        modelAndView.addObject("module", module);
        modelAndView.addObject("superior", superior);
        return modelAndView;
    }

    @RequestMapping(value = "getpage", method = RequestMethod.GET)
    public Map<String, Object> getPage(HttpServletRequest request) {
        Map<String, Object> map = new HashMap<String, Object>();
        int start = Integer.parseInt(request.getParameter("start"));
        int length = Integer.parseInt(request.getParameter("length"));
        String key = request.getParameter("key");
        Account a = this.getMyAccount();
        String name="singlePump"; 
        Module module = this.getPageModule(name);
        String dataFilter=module.getDataFilter();
        String superior = a.getUser().getSaleArea().getSuperior();
        List<SinglePump> SinglePumps = singlePumpService.getSinglePumps(start, length, key, superior,dataFilter, null, null);
        long count=0;
        if(dataFilter.equals("所有")){
        	count = singlePumpService.getRepository().getCount(key);
        }else if(dataFilter.equals("本部门")){
        	count = singlePumpService.getRepository().getCount(superior, key); 
        }
        map.put("data", SinglePumps);
        map.put("draw", request.getParameter("draw"));
        map.put("recordsFiltered", count);
        map.put("recordsTotal", count);
        return map;
    }

    @RequestMapping(value = "/savePrice", method = RequestMethod.POST)
    public Map<String, Object> saveSinglePump(HttpServletRequest request, @RequestBody SinglePump singlePump) {
        Map<String, Object> map = new HashMap<String, Object>();
        Device device=new Device();
        String deviceId=singlePump.getDevice().getId();
        device=deviceService.getRepository().findOne(deviceId);
        singlePump.setDevice(device);
        singlePumpService.getRepository().save(singlePump);
        map.put("code", 0);
        return map;
    }

    /**
     * EXCEL导入处理 *
     */
    @RequestMapping(value = "/import", method = RequestMethod.POST)
    public Map<String, Object> importExcel(@RequestParam("file") MultipartFile file,
                                           HttpServletRequest request, RedirectAttributes redirectAttributes) {

        Map<String, Object> map = new HashMap<String, Object>();
        int code = -1;
        long count = -1;
        Account a = this.getMyAccount();
        String superior = a.getUser().getSaleArea().getSuperior();
        String saveFileName = attachmentService.upload(file);
        //文件传后，再读取EXCEL写入数据库
        if (StringUtils.hasText(saveFileName)) {
            count = singlePumpService.importExcel(saveFileName, superior);
            code = 0;
        }
        map.put("code", code);
        map.put("count", count);
        return map;
    }

   
}
